#!/usr/bin/python
# coding=utf-8
import ConfigParser
import time

import RPi.GPIO as GPIO

from config import Config

'''
凹槽红外开关
'''


class PhotoElectricSensor:

    def __init__(self, gpio):
        self.gpio = int(gpio)
        GPIO.setmode(GPIO.BCM)  # 使用BCM编码方式
        # 设置引脚为输入和输出
        GPIO.setwarnings(False)
        # 设置针脚为输入，接到红外避障传感器模块的out引脚
        GPIO.setup(self.gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    def checkObstacle(self):
        '''
         是否有障碍物，如果有返回1，如果没有返回0
         :return: 1/0
        '''
        if GPIO.input(self.gpio) == False:
            return 1
        else:
            return 0


if __name__ == '__main__':
    # 调用简单demo
    try:
        while True:
            photoElectricSensor = PhotoElectricSensor(Config().read("PhotoElectric", "SAL_pin"))
            if photoElectricSensor.checkObstacle() == 1:  # 有障碍物警告
                print('warning.....')
                time.sleep(0.5)
            else:  # 无障碍物提示
                print('ok......')
                time.sleep(0.5)
    except KeyboardInterrupt:
        GPIO.cleanup()
